package com.neuedu.his.controller;

import org.springframework.web.bind.annotation.*;
import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/api")
public class LoginController {

    @PostMapping("/login")
    public Map<String, Object> login(@RequestBody LoginRequest request) {
        System.out.println("收到登录请求: " + request.getUsername());

        Map<String, Object> result = new HashMap<>();

        try {
            // 验证用户（实际项目中应调用服务层）
            if ("admin".equals(request.getUsername()) && "admin123".equals(request.getPassword())) {
                result.put("code", 200);
                result.put("message", "登录成功");

                Map<String, Object> user = new HashMap<>();
                user.put("id", 1);
                user.put("username", request.getUsername());
                user.put("role", "ADMIN");
                result.put("data", user);
            } else {
                result.put("code", 401);
                result.put("message", "用户名或密码错误");
            }
        } catch (Exception e) {
            result.put("code", 500);
            result.put("message", "登录失败: " + e.getMessage());
        }

        return result;
    }
}

// 登录请求对象
class LoginRequest {
    private String username;
    private String password;

    // Getters and Setters
    public String getUsername() { return username; }
    public void setUsername(String username) { this.username = username; }
    public String getPassword() { return password; }
    public void setPassword(String password) { this.password = password; }
}